package com.bootdo.wx.controller;

import java.util.*;

import cn.hutool.core.bean.BeanUtil;
import com.bootdo.common.utils.excel.LiveExcelUtils;
import com.bootdo.wx.domain.GoodsGoodsDO;
import com.bootdo.wx.domain.GoodsLiveDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.wx.domain.GoodsLiveAppointmentDO;
import com.bootdo.wx.service.GoodsLiveAppointmentService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.UuidUtil;
import com.bootdo.common.utils.StringUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * 
 * @author long
 * @email 499626561@qq.com
 * @date 2021-05-22 16:51:54
 */
 
@Controller
@RequestMapping("/wx/goodsLiveAppointment")
public class GoodsLiveAppointmentController {
	@Autowired
	private GoodsLiveAppointmentService goodsLiveAppointmentService;
	
	@GetMapping()
	@RequiresPermissions("wx:goodsLiveAppointment:goodsLiveAppointment")
	String GoodsLiveAppointment(){
	    return "wx/goodsLiveAppointment/goodsLiveAppointment";
	}
	@GetMapping("/{liveId}")
	@RequiresPermissions("wx:goodsLiveAppointment:goodsLiveAppointment")
	String GoodsLiveAppointment(@PathVariable("liveId") String liveId,Model model){
	    model.addAttribute("liveId",liveId);
		return "wx/goodsLiveAppointment/goodsLiveAppointment";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:goodsLiveAppointment:goodsLiveAppointment")
	public PageUtils list(@RequestParam Map<String, Object> params){
	    params.put("delFlag","0");
	    params.put("sort","ca_at");
	    params.put("order","desc");
        //查询列表数据
        Query query = new Query(params);
	    if(params.containsKey("name")&&StringUtils.isNotBlank(params.get("name").toString())){
            List<GoodsLiveAppointmentDO> goodsLiveAppointmentList = goodsLiveAppointmentService.likeList(query);
            int total = goodsLiveAppointmentList.size();
            PageUtils pageUtils = new PageUtils(goodsLiveAppointmentList, total);
            return pageUtils;
		}

	    if(params.containsKey("liveId")&&StringUtils.isNotBlank(params.get("liveId").toString())){
            List<Map<String,Object>> list = goodsLiveAppointmentService.getAppointmentListByLiveId(params);
            int total = list.size();
            PageUtils pageUtils = new PageUtils(list, total);
            return pageUtils;
        }
		List<GoodsLiveAppointmentDO> goodsLiveAppointmentList = goodsLiveAppointmentService.list(query);
		int total = goodsLiveAppointmentService.count(query);
		PageUtils pageUtils = new PageUtils(goodsLiveAppointmentList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wx:goodsLiveAppointment:add")
	String add(){
	    return "wx/goodsLiveAppointment/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:goodsLiveAppointment:edit")
	String edit(@PathVariable("id") String id,Model model){
		GoodsLiveAppointmentDO goodsLiveAppointment = goodsLiveAppointmentService.get(id);
		model.addAttribute("goodsLiveAppointment", goodsLiveAppointment);
	    return "wx/goodsLiveAppointment/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:goodsLiveAppointment:add")
	public R save( GoodsLiveAppointmentDO goodsLiveAppointment){
	    goodsLiveAppointment.setId(UuidUtil.get32UUID());
	    goodsLiveAppointment.setDelFlag(0);
	    goodsLiveAppointment.setCaAt(new Date());
		if(goodsLiveAppointmentService.save(goodsLiveAppointment)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:goodsLiveAppointment:edit")
	public R update( GoodsLiveAppointmentDO goodsLiveAppointment){
		goodsLiveAppointment.setUpAt(new Date());
		goodsLiveAppointmentService.update(goodsLiveAppointment);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:goodsLiveAppointment:remove")
	public R remove( String id){
		GoodsLiveAppointmentDO goodsLiveAppointment = goodsLiveAppointmentService.get(id);
		goodsLiveAppointment.setDelFlag(1);
		goodsLiveAppointment.setUpAt(new Date());
		if(goodsLiveAppointmentService.update(goodsLiveAppointment)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:goodsLiveAppointment:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
	   if(ids.length>0){
			for (String string : ids) {
				GoodsLiveAppointmentDO goodsLiveAppointment = goodsLiveAppointmentService.get(string);
				goodsLiveAppointment.setDelFlag(1);
				goodsLiveAppointment.setUpAt(new Date());
				goodsLiveAppointmentService.update(goodsLiveAppointment);
			}
		}
	
		return R.ok();
	}

	/**
	 * 导出用户excel表格
	 */
	@RequestMapping( "/exportExcel/{liveId}")
	@RequiresPermissions("wx:memberUser:memberUser")
	public void exportExcel(@PathVariable("liveId") String liveId, HttpServletResponse response, HttpServletRequest request){
		try
		{

			Map<String,Object> map = new HashMap<>();
			map.put("liveId",liveId);
			List<Map<String,Object>> list = goodsLiveAppointmentService.getAppointmentListByLiveId(map);
			//这里设置的文件格式是application/x-excel
			response.setContentType("application/octet-stream");
			response.setHeader("Content-Disposition", "attachment;filename=" + new String("预约信息列表.xls".getBytes(), "ISO-8859-1"));
			ServletOutputStream outputStream = response.getOutputStream();
			LiveExcelUtils.exportAppointmentExcel(list, outputStream);
			if(outputStream != null) {
				outputStream.close();
			}
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}
